package hammersmith.local.management;

import hammersmith.local.valueobjects.Customer;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author hammersmith
 * CustomerManegement-Klasse
 *
 */
/**
 * @author fboettger
 *
 */
public class CustomerManagement implements Serializable {

	private static final long serialVersionUID = 2630906428958726712L;
	private ArrayList<Customer> customerList;

	//Konstruktor
	public CustomerManagement() {
		customerList = new ArrayList<Customer>();
	}

	/**
	 * 
	 * Methode zum Erstellen eines neuen Kunden (Registrierung)
	 * 
	 * @param handler
	 * @param shop
	 * @param name
	 * @param firstname
	 * @param street
	 * @param zip
	 * @param city
	 * @param mail
	 * @param pw
	 * @return
	 */
	public int addNewCustomer(String name, String firstname, String street,
			String zip, String city, String mail, String pw) {
		
		Customer customer;
		int high = 9999;
		int low = 1000;
		boolean ok = false;

		while (!ok) {
		
			//Zufalls-ID errechnen (1000-9999)
			int id = (int) (Math.random() * (high - low) + low);

			customer = new Customer(name, firstname, street, zip, city, id,
					mail, pw);

			//Wenn ID schon vergeben wurde -> false -> negative Meldung 
			//Wenn ID nicht vergeben -> true -> positive Meldung
			for (Customer current : this.customerList) {
				if (customer.getCustomerId() == current.getCustomerId()) {
					ok = false;
				} else {
					ok = true;
					//hinzufuegen des neuen Kundenobjekts zur Liste
					this.customerList.add(customer);
					return id;
				}
			}
		}
		return 0;
	}

	public ArrayList<Customer> getCustomerList() {
		return customerList;
	}

	
	/**
	 * 
	 * Methode zum einloggen in das Kunden-Menue
	 * 
	 * @param user
	 * @param pw
	 * @return
	 */
	public Customer loginAsCustomer(String user, String pw) {
		int userAsInt = Integer.parseInt(user);

		//Durchlauf der aktuellen Kundenliste und abgleichen mit den Eingaben
		for (Customer customer : this.customerList) {
			if (userAsInt == customer.getCustomerId()) {
				if (pw.equals(customer.getPassword())) {
					return customer;

				} else {
					return null;
				}
			}
		}
		return null;
	}

	public void setCustomerList(ArrayList<Customer> customerList) {
		this.customerList = customerList;
	}
	
	public Customer getCustomerById(int id){
		
		for (Customer c : this.customerList) {
			if(id == c.getCustomerId()){
				return c;
			}
		}
		return null;
	}
	
}
